package DataBase;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;

public class DataDelete extends JInternalFrame {
	
	Connection conn;
	boolean flag = false;
	
	private JButton button;
	private JLabel label;
	private JTextField ea;
	private JTextField code;
	private JLabel label_1;
	private JLabel label_2;
	private JTextField serial;
	private JButton button_1;
	private JLabel status;
	
	/**
	 * Launch the application.
	 */
//	public static void main(String[] args) {
//		EventQueue.invokeLater(new Runnable() {
//			public void run() {
//				try {
//					DataDelete frame = new DataDelete();
//					frame.setVisible(true);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
//	}

	/**
	 * Create the frame.
	 */
	public DataDelete(Connection conn) {
		super("삭제", true, true, true, false);
		this.conn = conn;
		setBounds(350, 0, 325, 240);
		getContentPane().setLayout(null);
		getContentPane().add(getButton());
		getContentPane().add(getLabel());
		getContentPane().add(getEa());
		getContentPane().add(getCode());
		getContentPane().add(getLabel_1());
		getContentPane().add(getLabel_2());
		getContentPane().add(getSerial());
		getContentPane().add(getButton_1());
		getContentPane().add(getStatus());

	}
	private JButton getButton() {
		if (button == null) {
			button = new JButton("삭  제");
			button.setVisible(false);
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					int s = Integer.parseInt(serial.getText());
					String sql = "delete from ptbl where serial = ?";
						try{
							PreparedStatement pstmt =
									conn.prepareStatement(sql);
							pstmt.setInt(1, s);
							pstmt.executeUpdate();
							
							pstmt.close();
							status.setText("Serial [ "+ s +" ] 의 데이터가 삭제되었습니다.");
							serial.setText("");
							code.setText("");
							ea.setText("");
							serial.requestFocus();
							button.setVisible(false);

						}catch (Exception ex){
							ex.printStackTrace();
						}
				}
			});
			button.setFont(new Font("나눔바른고딕", Font.BOLD, 15));
			button.setBounds(104, 136, 97, 23);
		}
		return button;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("Ea");
			label.setFont(new Font("Verdana", Font.BOLD, 20));
			label.setBounds(29, 92, 63, 34);
		}
		return label;
	}
	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setDisabledTextColor(Color.BLACK);
			ea.setEnabled(false);
			ea.setColumns(10);
			ea.setBounds(104, 99, 116, 21);
		}
		return ea;
	}
	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setDisabledTextColor(Color.BLACK);
			code.setEnabled(false);
			code.setColumns(10);
			code.setBounds(104, 58, 116, 21);
		}
		return code;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("Code");
			label_1.setFont(new Font("Verdana", Font.BOLD, 20));
			label_1.setBounds(29, 48, 63, 34);
		}
		return label_1;
	}
	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("Serial");
			label_2.setFont(new Font("Verdana", Font.BOLD, 20));
			label_2.setBounds(29, 10, 76, 34);
		}
		return label_2;
	}
	private JTextField getSerial() {
		if (serial == null) {
			serial = new JTextField();
			serial.setColumns(10);
			serial.setBounds(104, 17, 116, 21);
		}
		return serial;
	}
	private JButton getButton_1() {
		if (button_1 == null) {
			button_1 = new JButton("조회");
			button_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					
					if(!serial.getText().trim().equals("")){
						try{
							int s = Integer.parseInt(serial.getText());
							String sql = "select * from ptbl where serial = ?";
							PreparedStatement pstmt =
									conn.prepareStatement(sql);
							pstmt.setInt(1, s);
							ResultSet rs = pstmt.executeQuery();
							if(rs.next()){
								code.setText(rs.getString("code"));
								ea.setText(""+rs.getInt("ea"));
								status.setText("Serial [ "+s+" ] 의"+"'"
												+code.getText()+"'가 검색되었습니다.");
								code.requestFocus();
								button.setVisible(true);
							}else{
								status.setText("Serial [ "+s +" ] 의 데이터가 검색되지 않았습니다.");
								code.setText("");
								ea.setText("");
								serial.setText("");
								button.setVisible(false);
							}
							rs.close();
							pstmt.close();
						}catch (Exception ex){
							ex.printStackTrace();
							JOptionPane.showMessageDialog(null, "Serial번호는 숫자를 입력하셔야 해요.");
						}
					}else{
						JOptionPane.showMessageDialog(null, "Serial번호를 입력해주세요.");
						code.setText("");
						ea.setText("");
					}
					serial.requestFocus();
				}
			});
			button_1.setFont(new Font("나눔바른고딕", Font.BOLD, 15));
			button_1.setBounds(231, 16, 61, 23);
		}
		return button_1;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel("Serial을 조회 후 삭제 할 수있습니다.");
			status.setBounds(12, 177, 280, 15);
		}
		return status;
	}
}
